In [1]:
print("hello world")


hello world

In [2]:
x=7

In [3]:
x.bit_length()


Out[3]:
3

In [4]:
x=3.14159

In [5]:
type(x)


Out[5]:
float

In [6]:
x=0.25

In [7]:
x.as_integer_ratio()


Out[7]:
(1, 4)

In [8]:
x="hello"

In [9]:
type(x)


Out[9]:
str

In [10]:
x.capitalize()


Out[10]:
'Hello'

In [11]:
x='world'

In [12]:
print(x)


world

In [13]:
x="""
Hello
World

"""
print(x)


Hello
World


Tuple


In [14]:
x=(2,3,"text")

In [15]:
print(x)


(2, 3, 'text')

In [16]:
x[2]


Out[16]:
'text'

In [17]:
x[-1]


Out[17]:
'text'

In [18]:
x[-2]


Out[18]:
3

List


In [19]:
x=[1,3,5,"hello"]

In [20]:
print(x)


[1, 3, 5, 'hello']

In [21]:
x.append("world")

In [22]:
x


Out[22]:
[1, 3, 5, 'hello', 'world']

In [23]:
x.pop()


Out[23]:
'world'

In [24]:
x


Out[24]:
[1, 3, 5, 'hello']

Dictionary


In [25]:
x={"key":"value", "cat":"a small animal" }

In [26]:
x


Out[26]:
{'cat': 'a small animal', 'key': 'value'}

In [27]:
x['cat']


Out[27]:
'a small animal'

In [28]:
x['dog']="a bigger animal"

In [29]:
x


Out[29]:
{'cat': 'a small animal', 'dog': 'a bigger animal', 'key': 'value'}

In [30]:
x.get('duck','not found')


Out[30]:
'not found'

for loop


In [31]:
for i in [1,2,3]:
    print(i)


1
2
3

In [32]:
for i in range(0,10,2):
    print(i)


0
2
4
6
8

while loop


In [33]:
i=0
while i<5:
    i+=1
    print i


1
2
3
4
5

function


In [34]:
def f(x):
    y=2*x
    return y

In [35]:
f(3)


Out[35]:
6

In [36]:
g=f

In [37]:
g(5)


Out[37]:
10

comprehension


In [38]:
x=[]
for i in range(1,13):
    x.append(i*25)

In [39]:
x


Out[39]:
[25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300]

In [40]:
%%timeit -n1
x=[]
for i in range(1,13000000):
    x.append(i*25)


1 loop, best of 3: 6.3 s per loop

In [41]:
x=[i*25 for i in range(1,13)]

In [42]:
x


Out[42]:
[25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300]

In [43]:
%%timeit -n1
x=[i*25 for i in xrange(1,13000000)]


1 loop, best of 3: 3.19 s per loop

In [44]:
x={i:i*25 for i in range(1,13)}

In [45]:
x


Out[45]:
{1: 25,
 2: 50,
 3: 75,
 4: 100,
 5: 125,
 6: 150,
 7: 175,
 8: 200,
 9: 225,
 10: 250,
 11: 275,
 12: 300}

homework

Find Fibonacci numbers f(x) when using the input from the given file.

\begin{eqnarray*} f(1)&=&1\\ f(2)&=&1\\ f(x)&=&f(x-1)+f(x-2)\\ \end{eqnarray*}

In [46]:
with open("fibo_input.txt","r") as fp:
    s=fp.read()
    x=[int(i) for i in s.split()]

In [47]:
x


Out[47]:
[2, 3, 10, 27, 48, 56, 75, 89, 92]

In [ ]: